package h0731;

import java.util.*;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 找朋友
 * @date 2024/7/31 16:55
 */
public class FindFriends {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //n个小朋友
        int n = sc.nextInt();

        int[] arr = new int[n];
        //n个小朋友身高
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        sc.close();

        getRes2(arr);
    }

    private static void getRes(int[] arr) {
        List<Integer> res = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            boolean temp = true;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] > arr[i]) {
                    res.add(j);
                    temp = false;
                    break;
                }
            }
            if (temp) res.add(0);
        }
        System.out.println(res);
    }

    //使用一个栈
    private static void getRes2(int[] arr) {
        LinkedList<Integer> stack = new LinkedList<>();
        int[] ans = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
//            if (stack.isEmpty() || arr[stack.peekLast()] > arr[i]) {
//                stack.addLast(i);
//            } else if (arr[stack.peekLast()] < arr[i]) {
//                Integer index = stack.removeLast();
//                ans[index] = i;
//                //如果大于,弹出栈顶索引，压入当前索引。如果栈
//                while (!stack.isEmpty()&&arr[stack.peekLast()] < arr[i]) {
//                    Integer in = stack.removeLast();
//                    ans[in] = i;
//                }
//                stack.addLast(i);
//            }
            //如果大于,弹出栈顶索引，压入当前索引。如果栈不为null，继续判断栈顶是否小于当前数
            while (!stack.isEmpty() && arr[stack.peekLast()] < arr[i]) {
                Integer in = stack.removeLast();
                ans[in] = i;
            }
            stack.addLast(i);
        }
        while (!stack.isEmpty()) {
            Integer i = stack.removeLast();
            ans[i] = 0;
        }
        StringJoiner joiner = new StringJoiner(" ");
        for (int a : ans) {
            joiner.add(a + "");
        }
        System.out.println(joiner);
    }

}
